package com.orhon.smartcampus.modules.student.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.student.dao.EclassDao;
import com.orhon.smartcampus.modules.student.dao.EclassRecordsDao;
import com.orhon.smartcampus.modules.student.dao.SInformationDao;
import com.orhon.smartcampus.modules.student.entity.Eclass;
import com.orhon.smartcampus.modules.student.entity.EclassRecords;
import com.orhon.smartcampus.modules.student.entity.SInformation;
import com.orhon.smartcampus.modules.student.gqlInputModel.GQLEclassRecordsInput;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.dao.UsersDao;
import com.orhon.smartcampus.modules.user.entity.Users;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddEclassRecordsMutation implements DataFetcher {


	@Autowired
	EclassRecordsDao recordsDao;
	@Autowired
    SInformationDao sInformationDao;
	@Autowired
    UsersDao usersDao;
	@Autowired
    EclassDao eclassDao;


	@Autowired
	TokenService tokenService;

	/**
	 * 新增/修改班级信息eureka:
  client:
    service-url:
      defaultZone: http://192.168.2.135:8010/eureka
	 * editor : ths
	 */
	@Override
    @Transactional
	public Object get(DataFetchingEnvironment environment) throws Exception {

		String token = tokenService.getSystemToken();
		Long schoolId = JwtUtils.getSchoolId(token);
		Long userId = JwtUtils.getUserId(token);
		String json = JSON.toJSONString(environment.getArgument("inputData"));
		GQLEclassRecordsInput input = JSON.parseObject(json , GQLEclassRecordsInput.class);
//		GQLEclassRecordsInput input = EclassRecordsInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));
		EclassRecords eclassRecords = null;
		if (input.getId() != null){
			eclassRecords = recordsDao.get(EclassRecords.class , input.getId());
			eclassRecords.setUpdatedAt(new Date());
			eclassRecords.setUpdatedBy(Integer.parseInt(userId.toString()));
		}
		else{
			eclassRecords = new EclassRecords();
			eclassRecords.setCreatedAt(new Date());
			eclassRecords.setCreatedBy(Integer.parseInt(userId.toString()));
		}
		
		if(input.getStudent_id() != null) {
			eclassRecords.setStudent(sInformationDao.get(SInformation.class, input.getStudent_id()));
		}
		if(input.getUser_id() != null) {
			eclassRecords.setUsers(usersDao.get(Users.class, input.getUser_id()));
		}
		if(input.getOriginalEclass() != null){

			eclassRecords.setOriginalEclass(eclassDao.get(Eclass.class, input.getOriginalEclass()));
		}
		if(input.getStatus() != null){

			eclassRecords.setStatus(input.getStatus());
		}
		if(input.getGoEclass() != null){

			eclassRecords.setGoEclass(eclassDao.get(Eclass.class, input.getGoEclass()));
		}
		if(input.getTransactionType()!= null){
			eclassRecords.setTransactionType(input.getTransactionType());
		}
		
		if(input.getEclassTime() != null){
			eclassRecords.setEclassTime(input.getEclassTime());
		}
		if(input.getReason() != null){
			eclassRecords.setReason(input.getReason());
		}
		
		try{
			recordsDao.saveOrUpdate(eclassRecords);
		}
		catch (Exception e){
		    e.printStackTrace();
			return Result.error(500,""+e);
		}
		return eclassRecords;
	}
}

